package com.springboot2.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.springboot2.controller.utils.R;
import com.springboot2.domain.Book;
import com.springboot2.service.IBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
//
import org.ofdrw.core.annotation.pageannot.AnnotType;
import org.ofdrw.font.FontName;
import org.ofdrw.layout.OFDDoc;
import org.ofdrw.layout.edit.Annotation;
import org.ofdrw.layout.element.canvas.FontSetting;
import org.ofdrw.reader.OFDReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;


import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import java.util.List;

@Slf4j
@Api(tags = "mybatisPlus编写 图纸相关接口", description = "提供图纸信息的测试Rest API")
@RestController
@RequestMapping("/books")
public class BookController2 {

    /**
     * 加入印章类型注释对象
     */
    @Test
    @CrossOrigin("http://localhost:9527")
    @GetMapping("/ofd")
    void addAnnotationStamp() throws IOException {
//        也可以直接传递
        long startTime = System.currentTimeMillis();
        for (int k = 2; k <= 75; k++) {
        String name = "aa.ofd";
            System.out.println(name);
        Path srcP = Paths.get("cxy/src/test/resources", name);

        System.out.println("srcP" + srcP.toAbsolutePath());
        String outName = "cxy/target/AddAnnotationStamp"+k+".ofd";
//            Path outP = Paths.get("cxy/target/AddAnnotationStamp.ofd");
            Path outP = Paths.get(outName);


        Path imgPath = Paths.get("cxy/src/test/resources", "StampImg.png");
        System.out.println("imgPath" + imgPath);

        try (OFDReader reader = new OFDReader(srcP);
             OFDDoc ofdDoc = new OFDDoc(reader, outP)) {
            Annotation annotation = new Annotation(60d, 10d, 200d, 200d, AnnotType.Stamp, ctx -> {
                ctx.setGlobalAlpha(0.53);
                ctx.drawImage(imgPath, 0, 0, 40d, 40d);
                FontSetting setting = new FontSetting(20, FontName.SimSun.font());

                ctx.setFillColor(170, 160, 165)
                        .setFont(setting)
                        .setGlobalAlpha(0.8);

                for (int i = 0; i <= 6; i++) {
                    for (int j = 0; j <= 6; j++) {
                        ctx.save();
                        ctx.translate(40 * i, j * 60);
                        ctx.rotate(45);
                        ctx.fillText("曹雪洋", 10, 10);
                        ctx.restore();
                    }
                }
            });
              ofdDoc.addAnnotation(1, annotation);

        }
        System.out.println("生成文档位置：" + outP.toAbsolutePath());
        }
        System.out.println("耗时：" + (System.currentTimeMillis() - startTime) + "ms");
    }

//    private static  final Logger log = LoggerFactory.getLogger(BookController2.class);

    //1 的格式统一类
    @Autowired
    private IBookService bookService;

    /*
     * 查询全部
     * @param
     * @return
     * */
    @CrossOrigin("http://localhost:9527")
    @GetMapping
    @ApiOperation(value = "查询全部图纸", notes = "get查询全部图纸")
    public R getAll() {
//        return bookService.list();
        //     创建记录日志的对象

        log.debug("debug....");
        log.info("info....");
        log.warn("warn....");
        log.error("error....");

        return new R(true, bookService.list());
    }

    @CrossOrigin
    @PostMapping
    @ApiOperation(value = "新增图纸,不能传id 为修改 ", httpMethod = "POST")
    public R save(@RequestBody Book book) throws Exception {
//            R r = new R();
//            boolean flag = bookService.save(book);
//            r.setFlag(flag);
//        r 类中增加构造函数后 上三下一  可简化为一行
        System.out.println(book.getName());
        if (book.getName().equals("123")) throw new Exception();

        return new R(bookService.save(book));
//        return r;
    }

    //    update @RequestBody 为传递json
    @CrossOrigin("http://localhost:9527")
    @ApiOperation("更新图纸信息,json需要传id")
    @PutMapping
    public R update(@RequestBody Book book) {
//        return bookService.updateById(book);
        return new R(bookService.updateById(book));
    }

    @CrossOrigin("http://localhost:9527")
    @ApiOperation(value = "删除单个图纸", notes = "")
    @ApiImplicitParam(name = "id", value = "图纸的唯一id", required = true)
    @DeleteMapping("{id}")
    public R delete(@PathVariable Integer id) {
//        return bookService.deleteCxyBook(id);
        return new R(bookService.deleteCxyBook(id));
    }

    //    删除和 查询单个 一个用路径来
    @CrossOrigin
    @ApiOperation(value = "查询单个图纸", notes = "")
    @ApiImplicitParam(name = "id", value = "图纸的唯一id", required = true)
    @GetMapping("{id}")
    public R getById(@PathVariable Integer id) {
//        return bookService.getById(id);
        return new R(true, bookService.getById(id));
    }

    //    分页
//    @CrossOrigin
//    @ApiOperation(value = "分页查询图纸", notes = "get查询")
//    @ApiImplicitParams({@ApiImplicitParam(name = "currentPage", value = "当前页", required = true, defaultValue = "1"), @ApiImplicitParam(name = "pageSize", value = "每页大小", required = true, defaultValue = "5")})
//    @GetMapping("{currentPage}/{pageSize}")
//    public R getPage(@PathVariable int currentPage, @PathVariable int pageSize) {
////        return book Service.getPage(currentPage, pageSize);
//        IPage<Book> page = bookService.getPage(currentPage,pageSize);
//        if(currentPage > page.getPages()){
//            page  = bookService.getPage((int)page.getPages(),pageSize);
//        }
//
//        return new R(true,page);
//    }
//

    //    带条件分页
    @CrossOrigin
    @ApiOperation(value = "分页查询图纸", notes = "get查询")
    @ApiImplicitParams({@ApiImplicitParam(name = "currentPage", value = "当前页", required = true, defaultValue = "1"), @ApiImplicitParam(name = "pageSize", value = "每页大小", required = true, defaultValue = "5")})
    @GetMapping("{currentPage}/{pageSize}")
    public R getPage(@PathVariable int currentPage, @PathVariable int pageSize, String name, Book book) {
        System.out.println("参数" + book);
//        return book Service.getPage(currentPage, pageSize);
        IPage<Book> page = bookService.getPage(currentPage, pageSize, book);
        if (currentPage > page.getPages()) {
            page = bookService.getPage((int) page.getPages(), pageSize, book);
        }

        return new R(true, page);
    }


}
